package myservlet.control;
//退押金
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mybean.data.login;

public class returnDeposit extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");				
			}
			catch(Exception e) {}
		}
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		login loginBean=null; 
		HttpSession session= req.getSession(true);
		loginBean = (login)session.getAttribute("loginBean");
		String name=loginBean.getLogname();
		String condition="select * from orderform where username='"+name+"'";//查看当前用户是否有订单
		Connection con;
		//连接数据库
		String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8";
		try {
			con = DriverManager.getConnection(url,"root","744615");
			System.out.println("连接数据库成功");
			Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			ResultSet rs=sql.executeQuery(condition);
			if(rs.next()==true) {//用户有订单，还有车没还
				out.println("<script language = javascript>alert('你还有未还的车，不能退还押金!');");
				out.println("location.href='returnDeposit.jsp'</script>");
				return;
			}
			else {
				condition="update user set Udeposit='0' where Nickname='"+name+"'";//修改用户押金
				sql.executeUpdate(condition);
				out.println("<script language = javascript>alert('押金成功退还!');");
				out.println("location.href='homepage.jsp'</script>");
			}
			con.close();//关闭连接
		}
		catch (SQLException exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('退还押金失败!');");
			out.println("location.href='returnDeposit.jsp'</script>");
		}
	}
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}

